package org.jeecg.modules.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MqttMessageHandler {

    @Value("${mqtt.topic-prefix:ftts}")
    private String topicPrefix;

    /**
     * 处理接收到的消息
     * @param message
     */
    @ServiceActivator(inputChannel = "mqttInputChannel")
    public void handleMessage(Message<?> message) {
        // 获取消息内容
        Object payload = message.getPayload();
        // 获取主题信息
        String topic = String.valueOf(message.getHeaders().get("mqtt_receivedTopic"));
        // 获取服务质量
        Integer qos = (Integer) message.getHeaders().get("mqtt_receivedQos");

        System.out.println("Received message:");
        System.out.println("  Topic: " + topic);
        System.out.println("  QoS: " + qos);
        System.out.println("  Message: " + payload.toString());

        // 根据主题后缀处理不同逻辑
        handleTopicMessage(topic, payload.toString());
    }

    /**
     * 根据主题后缀处理不同逻辑
     * @param topic
     * @param message
     */
    private void handleTopicMessage(String topic, String message) {
        // 检查是否以配置的前缀开头
        if (topic.startsWith(topicPrefix + "/")) {
            // 提取主题后缀
            String topicSuffix = topic.substring(topicPrefix.length());

            // 根据不同的后缀处理不同的逻辑
            if (topicSuffix.startsWith("/device/")) {
                handleDeviceMessage(topicSuffix, message);
            } else if (topicSuffix.startsWith("/sensor/")) {
                handleSensorMessage(topicSuffix, message);
            } else if (topicSuffix.startsWith("/control/")) {
                handleControlMessage(topicSuffix, message);
            } else {
                handleDefaultMessage(topicSuffix, message);
            }
        }
    }

    /**
     * 处理设备相关消息
     * @param topicSuffix
     * @param message
     */
    private void handleDeviceMessage(String topicSuffix, String message) {
        System.out.println("Handling device message from topic suffix: " + topicSuffix);
        System.out.println("Message content: " + message);
        // 在这里添加设备相关的处理逻辑

        // 示例：如果是设备上传数据
        if (topicSuffix.endsWith("/upload")) {
            System.out.println("Processing device data upload...");
            // 处理设备上传的数据
        }
        // 其他设备相关逻辑...
    }

    // 处理传感器相关消息
    private void handleSensorMessage(String topicSuffix, String message) {
        System.out.println("Handling sensor message from topic suffix: " + topicSuffix);
        System.out.println("Message content: " + message);
        // 在这里添加传感器相关的处理逻辑

        // 示例：如果是传感器数据
        if (topicSuffix.endsWith("/data")) {
            System.out.println("Processing sensor data...");
            // 处理传感器数据
        }
        // 其他传感器相关逻辑...
    }

    // 处理控制相关消息
    private void handleControlMessage(String topicSuffix, String message) {
        System.out.println("Handling control message from topic suffix: " + topicSuffix);
        System.out.println("Message content: " + message);
        // 在这里添加控制相关的处理逻辑

        // 示例：如果是控制命令
        if (topicSuffix.endsWith("/command")) {
            System.out.println("Processing control command...");
            // 处理控制命令
        }
        // 其他控制相关逻辑...
    }

    // 处理默认消息
    private void handleDefaultMessage(String topicSuffix, String message) {
        System.out.println("Handling default message from topic suffix: " + topicSuffix);
        System.out.println("Message content: " + message);
        // 默认处理逻辑
    }

}


